package com.ytrsoft.utils;

import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class CodeKit {

    private CodeKit() {
        throw new UnsupportedOperationException();
    }

    public static String findClassName(String code) {
        Pattern pattern = Pattern.compile("(public\\s+)?class\\s+([\\w$_]+)");
        Matcher matcher = pattern.matcher(code);
        if (matcher.find()) {
            return matcher.group(2);
        }
        return null;
    }

    public static void invokeMain(Class<?> cls) throws Exception {
        try {
            Method main = cls.getMethod("main", String[].class);
            main.invoke(null, (Object) new String[]{});
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
